//日期工具
Date.prototype.format = function(fmt){
	var o = {
		"M+" : this.getMonth()+1,                 //月
		"d+" : this.getDate(),                    //日
		"h+" : this.getHours(),                   //时
		"m+" : this.getMinutes(),                 //分
		"s+" : this.getSeconds(),                 //秒
		"q+" : Math.floor((this.getMonth()+3)/3), //季
		"S"  : this.getMilliseconds()             //毫秒
	};
	if(/(y+)/.test(fmt))
		fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));
	for(var k in o)
		if(new RegExp("("+ k +")").test(fmt))
			fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));
	return fmt;
}

//弹出框
function xx_alert (alertType, msg){
	if(alertType == "success"){
		swal("操作成功!", msg, "success");
	}
	if(alertType == "error"){
		swal("操作失败!", msg, "error");
	}
	if(alertType == "info"){
		swal(msg);
	}
}

//弹出框-确认删除
function xx_alert_confirm(text, method){
	swal({
		title: "确定删除?",
		text: text,
		type: "warning",
		showCancelButton: true,
		confirmButtonColor: "#DD6B55",
		confirmButtonText: "确认删除!",
		cancelButtonText: "取消",
		closeOnConfirm: false,
		closeOnCancel: false
	},
	function(isConfirm){
		if (isConfirm) {
			method();
		}else {
			/*xx_alert("info", "取消删除！");*/
			swal.close();
		}
	});
}

//模态框清除数据
function xx_modal_clear(modalId) {
	$(modalId +" input").val("");
};

//模态框禁止编辑数据
function xx_modal_disable(modalId) {
	$(modalId +" input").attr("readonly", "readonly");
};


//参数说明: url 地址; data 数据; sucMethod 成功调用的方法;
function xx_ajax(url, type, data, dataType, continner, successMethod) {
	$.ajax({
		type: type,
		url: url,
		dataType: dataType,
		data: data,
		beforeSend: function () {
			$(continner).children("button").attr({"disabled": "disabled"});
		},
		complete: function () {
			$(continner).children("button").removeAttr("disabled");
			$("#ajax_modal").modal("hide");
		},
		success: function (response) {
			$(continner).children("button").removeAttr("disabled");
			$("#ajax_modal").modal("hide");
			successMethod(response);
		},
		error: function (response) {
			xx_alert("error", "请求错误！");
		}
	});
}

//回显
xx_view_load = (function () {
	var backViewObj = {};
	
	//general (input, select, textArea)
	backViewObj.general = function (formId, jsonArray, backViewType) {
		var i,json,key,value,$this;
		if (!backViewType) {
			backViewType = "input";
		}
		for (i = 0; i < jsonArray.length; i++) {
			json = jsonArray[i];
			for (key in json) {
				value = json[key];
				$(formId).find(backViewType + "[name=" + key + "]:not([type='radio']):not([type='checkbox'])").each(function () {
					$this = $(this);
					$this.val(value);
				});
			}
		}
	};
	
	//radio
	backViewObj.radio = function (formId, jsonArray) {
		var i,json,key,value,$this;
		for (i = 0; i < jsonArray.length; i++) {
			json = jsonArray[i];
			for (key in json) {
				value = json[key];
				$(formId).find("input[name=" + key + "]:radio").each(function () {
					$this = $(this);
					if ($this.val() == value) {
						$this.click();
					}
				});
			}
		}
	};
	
	//checkbox
	backViewObj.checkbox = function (formId, array, checkboxName) {
		var i,value,$this;
		for (i = 0; i < array.length; i++) {
			value = array[i];
			$(formId).find("input[name=" + checkboxName + "]:checkbox").each(function () {
				$this = $(this);
				if ($this.val() == value) {
					$this.prop("checked", true);
				}
			});
		}
	};
	
	//将key的首字母转换为大写 (基于general)
	backViewObj.firstKeyToUpper = function (formId, jsonArray, backViewType) {
		var i,json,key,value,$this;
		if (!backViewType) {
			backViewType = "input";
		}
		for (i = 0; i < jsonArray.length; i++) {
			json = jsonArray[i];
			for (key in json) {
				value = json[key];
				//特有代码-
				key = xx_string.firstToUpper(key);
				//特有代码/-
				$(formId).find(backViewType + "[name=" + key + "]:not([type='radio']):not([type='checkbox'])").each(function () {
					$this = $(this);
					$this.val(value);
				});
			}
		}
	};
	
	//自动解析json数据并回显(input&textArea, select, radio)
	backViewObj.auto = function (formId, json, jsonArray) {
		if (!xx_isBlank(json)) {
			if (xx_isBlank(jsonArray)) {
				jsonArray = [];
				jsonArray.push(json);
			}
		}
		if (!xx_isBlank(jsonArray)) {
			//input
			xx_view_load.general(formId, jsonArray, "input");
			//select
			xx_view_load.general(formId, jsonArray, "select");
			//radio
			xx_view_load.radio(formId, jsonArray);
			//textarea
			xx_view_load.general(formId, jsonArray, "textarea");
		}
	};
	return backViewObj;
})();

//是否为空
xx_isBlank = function (obj) {
	return obj == null || typeof(obj) == "undefined";
};

//string操作
xx_string = (function () {
	var stringObj = {};
	//将第一个字母转换为大写
	stringObj.firstToUpper = function (str) {
		return str.substr(0,1).toUpperCase() + str.substr(1);
	};
	//去掉第一个字符
	stringObj.subFirst = function (str) {
		return str.substr(1);
	};
	//string转数组
	stringObj.toArray = function (str) {
		var array = [];
		array.push(str);
		return array;
	};
	return stringObj;
})();

//关闭模态框
$(document).on("click", ".modal-close", function(){
	$(this).closest(".modal").modal('hide');
});

//关闭模态框
function xx_modal_close(modal){
	$(modal).modal("hide");
}

//打开模态框
function xx_modal_open(modal){
	$(modal).modal({backdrop: "static"});
}